package com.xb.cloud.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.user.model.EndUserAddress;
import com.xb.cloud.user.model.EndUserAddressExample;
import com.xb.cloud.user.model.mapper.EndUserAddressMapper;
import com.xb.cloud.user.service.client.EndUserAddressService;
import com.xb.cloud.user.service.client.bo.EndUserAddressBo;

@Service
public class EndUserAddressServiceImpl extends AbstractService implements EndUserAddressService {

	@Resource
	EndUserAddressMapper endUserAddressMapper;

	@Override
	@Transactional(value = "transactionManager_user", propagation = Propagation.REQUIRED)
	public int createtUserAddress(EndUserAddressBo addressBo) {

		if (addressBo.getIsDefault()) {
			// 新增地址为默认地址，修改所有地址为非默认
			Long companyId = addressBo.getCompanyId();
			Long endUserId = addressBo.getEndUserId();
			EndUserAddressBo defaultAddress = getDefaultAddress(companyId, endUserId);
			if (defaultAddress != null) {
				defaultAddress.setIsDefault(false);
				updateUserAddressById(defaultAddress);
			}
		}

		EndUserAddress endUserAddress = BeanUtils.newAndCopyProperties(EndUserAddress.class, addressBo);
		endUserAddress.setId(ipKeyGenerator.generateKey());
		endUserAddress.setIsDeleted(false);
		endUserAddress.setIsEnabled(true);
		endUserAddress.setCreateTime(timeService.getDate());
		endUserAddress.setCreateUserId(endUserAddress.getEndUserId());
		int inserted = endUserAddressMapper.insertSelective(endUserAddress);
		return inserted;
	}

	@Override
	public EndUserAddressBo getUserAddressById(Long addressId) {
		EndUserAddress endUserAddress = endUserAddressMapper.selectByPrimaryKey(addressId);
		return BeanUtils.newAndCopyProperties(EndUserAddressBo.class, endUserAddress);
	}

	@Override
	public List<EndUserAddressBo> getUserAddressListByUserId(Long companyId, Long endUserId) {
		EndUserAddressExample example = new EndUserAddressExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andEndUserIdEqualTo(endUserId)
				.andIsDeletedEqualTo(false).andIsEnabledEqualTo(true);
		List<EndUserAddressBo> userAddressList = endUserAddressMapper.getUserAddressListByUserId(example);
		return userAddressList;
	}

	@Override
	@Transactional(value = "transactionManager_user", propagation = Propagation.REQUIRED)
	public int updateUserAddressById(EndUserAddressBo addressBo) {
		if (addressBo.getIsDefault()) {
			// 地址为默认地址，修改所有地址为非默认
			Long companyId = addressBo.getCompanyId();
			Long endUserId = addressBo.getEndUserId();
			EndUserAddressBo defaultAddress = getDefaultAddress(companyId, endUserId);
			if (defaultAddress != null) {
				defaultAddress.setIsDefault(false);
				updateUserAddressById(defaultAddress);
			}
		}

		EndUserAddress endUserAddress = BeanUtils.newAndCopyProperties(EndUserAddress.class, addressBo);
		endUserAddress.setUpdateTime(timeService.getDate());
		int updated = endUserAddressMapper.updateByPrimaryKeySelective(endUserAddress);
		return updated;
	}

	@Override
	public int deleteUserAddressById(Long addressId) {
		EndUserAddress endUserAddress = new EndUserAddress();
		endUserAddress.setId(addressId);
		endUserAddress.setIsDeleted(true);
		int updated = endUserAddressMapper.updateByPrimaryKeySelective(endUserAddress);
		return updated;
	}

	@Override
	public EndUserAddressBo getDefaultAddress(Long companyId, Long endUserId) {
		EndUserAddressExample example = new EndUserAddressExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andEndUserIdEqualTo(endUserId).andIsDefaultEqualTo(true)
				.andIsDeletedEqualTo(false).andIsEnabledEqualTo(true);
		List<EndUserAddressBo> userAddressList = endUserAddressMapper.getUserAddressListByUserId(example);
		if (CollectionUtils.isEmpty(userAddressList)) {
			return null;
		}
		return userAddressList.get(0);
	}

}
